package com.ruoyi.core.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.core.domain.MaintenanceInfo;
import com.ruoyi.core.domain.custombean.ClientUserProductUsageRightQueryResult;
import com.ruoyi.core.domain.custombean.CustomTime;
import com.ruoyi.core.lock.ULock;
import com.ruoyi.core.mapper.ClientUserProductRelationMapper;
import com.ruoyi.core.mapper.CustomTimeMapper;
import com.ruoyi.core.service.CustomTimeService;
import com.ruoyi.core.service.MaintenanceInfoService;
import com.ruoyi.core.vo.CustomTimerDTO;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author xiaom
 * @Date 2020/12/22 14:02
 * @Version 1.0.0
 * @Description <>
 **/
@Service
public class CustomTimeServiceImpl implements CustomTimeService {

    @Resource
    private CustomTimeMapper customTimeMapper;

    @Resource
    private ClientUserProductRelationMapper clientUserProductRelationMapper;

    @Autowired
    private MaintenanceInfoService maintenanceInfoService;

    @Override
    @ULock
    public CustomTimerDTO addFunctionUseCount(Long productId, Integer type) {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        CustomTime customTime = getByUserIdAndProductIdAndType(productId, type);
        Long totalCount = getTotalUseCount(productId);
        if(customTime == null){
            //初始化
            customTime = new CustomTime();
            customTime.setMonth(DateUtils.getCurrentDateByMonth(new Date()));
            customTime.setProductId(productId);
            customTime.setUserId(userId);
            customTime.setType(type);
            customTime.setUseCnt(1L);
            customTimeMapper.insertSelective(customTime);
        }else{
            if(customTime.getUseCnt() >= totalCount){
                customTime.setUseCnt(customTime.getUseCnt());
            }else{
                customTime.setUseCnt(customTime.getUseCnt() + 1);
            }
            customTimeMapper.updateByPrimaryKeySelective(customTime);
        }
        CustomTimerDTO dto = new CustomTimerDTO();
        dto.setUsedCount(customTime.getUseCnt());
        dto.setTotalCount(totalCount);
        return dto;
    }

    private CustomTime getByUserIdAndProductIdAndType(Long productId, Integer type){
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        return customTimeMapper.selectByUserIdAndProductIdAndType(userId, productId, type,DateUtils.getCurrentDateByMonth(new Date()));
    }

    private Long getTotalUseCount(Long productId){
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        List<ClientUserProductUsageRightQueryResult> list = clientUserProductRelationMapper.findClientProductByClientUserId(userId);
        if(list == null || list.isEmpty()){
            return 0L;
        }
        int basisCount = 30;
        MaintenanceInfo basisInfo = maintenanceInfoService.findByKey("sys_used_basis_count");
        if(basisInfo != null){
            basisCount = Integer.parseInt(basisInfo.getValue());
        }
        ClientUserProductUsageRightQueryResult productUsageRight = list.stream().filter(item -> item.getProductId().equals(productId)).findFirst().get();
        return productUsageRight.getClientUserDurationDay() * basisCount;
    }

    @Override
    public CustomTimerDTO getFunctionUseCount(Long productId, Integer type) {
        CustomTime customTime = getByUserIdAndProductIdAndType(productId,type);

        CustomTimerDTO dto = new CustomTimerDTO();
        dto.setUsedCount(customTime == null ? 0L : customTime.getUseCnt());
        //计算总使用次数
        dto.setTotalCount(getTotalUseCount(productId));
        return dto;
    }
}
